library(readr)
library(plotly)
## Warning: package 'plotly' was built under R version 4.1.3
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggplot2)
library(DMwR2)
## Warning: package 'DMwR2' was built under R version 4.1.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.1.3
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(hrbrthemes)
## Warning: package 'hrbrthemes' was built under R version 4.1.3
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
#setwd("E:/QMSS/DV/Group_V_Global_Temperature/GlobalLandTemperaturesByCountry")
getwd()
## [1] "E:/QMSS/DV/Group_V_Global_Temperature"
Country<-read_csv("E:/QMSS/DV/Group_V_Global_Temperature/GlobalLandTemperaturesByCountry.csv")
## Rows: 577462 Columns: 4
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (1): Country
## dbl  (2): AverageTemperature, AverageTemperatureUncertainty
## date (1): dt
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
class(Country$dt)
## [1] "Date"
New_Country<-Country%>%
  filter(AverageTemperature!="N/A"& AverageTemperatureUncertainty!="N/A")
New_Country<-New_Country%>%
  filter(dt >="1970-01-01")
New_Country<-New_Country %>%
    group_by(Country) %>%
  
    mutate(Diff_Temp = AverageTemperature - lag(AverageTemperature,12))
USA<-New_Country%>%
  filter(Country %in% c("United States"))
class(USA$dt)
## [1] "Date"
Japan<-New_Country%>%
  filter(Country %in% c("Japan"))
Russia<-New_Country%>%
  filter(Country %in% c("Russia"))
plot1<- ggplot() +
    geom_line(data = Russia, aes(x=dt, y=AverageTemperature,color="AverageTemperature")) +
    geom_line(data = Russia, aes(x=dt, y=Diff_Temp,color="Diff_Temp"))+
    xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
    ggtitle("Russia Monthly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1990-01-01"),as.Date("2013-09-11")))+
   theme_minimal()

  
ggplotly(plot1)
#plot1
plot2<-ggplot()+
    geom_line(data = USA, aes(x=dt, y=AverageTemperature,color="AverageTemperature")) +
    geom_line(data = USA, aes(x=dt, y=Diff_Temp,color="Diff_Temp"))+
    xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
  ggtitle("USA Monthly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1990-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(plot2)
plot3<-ggplot()+
    geom_line(data = Japan, aes(x=dt, y=AverageTemperature,color="AverageTemperature")) +
    geom_line(data = Japan, aes(x=dt, y=Diff_Temp,color="Diff_Temp"))+
    xlab("Dates")+
    ylab("Tempt")+
   ggtitle("Japan Monthly Temperature and Difference with previous year from 1990 to 2013")+
   labs(color="Tempt_Type")+
    scale_x_date(limit=c(as.Date("1990-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(plot3)
avg_diff<-New_Country%>%
  group_by(Country)%>%
  summarise(Variance = var(AverageTemperature))%>%
  arrange(Variance)  %>%
  mutate(rank = row_number())%>%
  filter(rank<=10)
class(New_Country$Diff_Temp)
## [1] "numeric"
avg_diff_by_tempdiff<-New_Country%>%
  filter(Diff_Temp!="N/A")%>%
  group_by(Country)%>%
  summarise(meanD = mean(Diff_Temp))
Singapore<-New_Country%>%
  filter(Country %in% c("Singapore"))
plot4<-ggplot()+
    geom_line(data = Singapore, aes(x=dt, y=AverageTemperature,color="AverageTemperature")) +
    geom_line(data = Singapore, aes(x=dt, y=Diff_Temp,color="Diff_Temp"))+
    xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
   ggtitle("Singapore Monthly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1971-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(plot4)
Indonesia<-New_Country%>%
  filter(Country %in% c("Colombia"))
plot4<-ggplot()+
    geom_line(data = Indonesia, aes(x=dt, y=AverageTemperature,color="AverageTemperature")) +
    geom_line(data = Indonesia, aes(x=dt, y=Diff_Temp,color="Diff_Temp"))+
    xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
   ggtitle("Columbia Monthly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1971-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(plot4)
By_Year<-New_Country%>%
  mutate(year = substring(dt,0,4))%>%
  group_by(Country,year)%>%
  summarise(Year_Tempt = mean(AverageTemperature))%>%
  mutate(Year_diff = Year_Tempt-lag(Year_Tempt))
## `summarise()` has grouped output by 'Country'. You can override using the
## `.groups` argument.
By_Year$year<-ymd(By_Year$year, truncated = 2L)
class(By_Year$year)
## [1] "Date"
By_Year_USA<-By_Year%>%
  filter(Country %in% c("United States"))
Year_1<-ggplot()+
    geom_line(data = By_Year_USA, aes(x=year, y=Year_Tempt,color="Year_Tempt")) +
    geom_line(data = By_Year_USA, aes(x=year, y=Year_diff,color="Year_diff"))+
      xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
   ggtitle("USA Yearly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1971-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(Year_1)
By_Year_Russia<-By_Year%>%
  filter(Country %in% c("Russia"))
Year_2<-ggplot()+
    geom_line(data = By_Year_Russia, aes(x=year, y=Year_Tempt,color="Year_Tempt")) +
    geom_line(data = By_Year_Russia, aes(x=year, y=Year_diff,color="Year_diff"))+
      xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
   ggtitle("Russia Yearly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1971-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(Year_2)
By_Year_Japan<-By_Year%>%
  filter(Country %in% c("Japan"))
Year_3<-ggplot()+
    geom_line(data = By_Year_Japan, aes(x=year, y=Year_Tempt,color="Year_Tempt")) +
    geom_line(data = By_Year_Japan, aes(x=year, y=Year_diff,color="Year_diff"))+
      xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
   ggtitle("JP Yearly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1971-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(Year_3)
Continents<-New_Country%>%
  filter(Country %in% c("Asia", "Africa", "North America", "South America", "Antarctica", 'Europe','Oceania'))
Vio_1<-ggplot(data = Continents, aes(x= Country,y=AverageTemperature,fill=Country))+
  geom_violin() +
  geom_boxplot(width=0.1, color="grey", alpha=0.2)+
   theme_minimal()
  
ggplotly(Vio_1)
library(tidygeocoder)
## Warning: package 'tidygeocoder' was built under R version 4.1.3
#coord <- geo(By_Year$Country)
#write.csv(coord,"E:/QMSS/DV/Group_V_Global_Temperature/coord.csv", row.names = FALSE,fileEncoding = "UTF-8")
#coord<-read_csv("E:/QMSS/DV/Group_V_Global_Temperature/coord.csv")
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-28, (SVN revision 1158)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: E:/R-4.1.2/library/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: E:/R-4.1.2/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-6
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was E:/R-4.1.2/library/rgdal/proj
# world_shape = readOGR(dsn= "E:/QMSS/DV/5069/course_content/Lectures/Week07/data/world_map", 
#                      layer="TM_WORLD_BORDERS_SIMPL-0.3")

{r} # By_Year<-By_Year%>% # filter(Year_diff!="N/A") #

{r} # combined <- world_shape@data%>% # left_join(By_Year,by = c('NAME' = 'Country')) #

{r} # Wmerged <-world_shape # Wmerged@data <- combined #

world <- map_data("world") %>%
  filter(region != "Antarctica")
library(viridis)
## Loading required package: viridisLite
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.1.3
library(gifski)
## Warning: package 'gifski' was built under R version 4.1.3
By_year_new <-By_Year%>%
  filter(Year_diff!="N/A")%>%
   right_join(world, by= c("Country" = "region"))%>%
    ggplot(aes(long, lat, 
             group= group, 
             fill= Year_diff)) +
  geom_polygon(color = "white", 
               size = 0.01)+
  theme_void() +
  scale_fill_viridis(option = "B",
                     name= "Year_Diff",
                     guide = guide_colorbar(
                       direction = "horizontal",
                       barheight = unit(2, units = "mm"),
                       barwidth = unit(100, units = "mm"),
                       draw.ulim = FALSE,
                       title.position = "top",
                       title.hjust = 0.5,
                       title.vjust = 0.5 ))+
  labs(title="Yearly Temperature Diff",
       subtitle = "{current_frame}")+
  theme(
    plot.title = element_text(size = 12, hjust = 0.5),
    plot.subtitle = element_text(size = 10, hjust = 0.5),
    plot.caption = element_text(size = 8, hjust = 1),
    legend.position = "bottom") +
  coord_fixed (ratio = 1.3) +
  transition_manual(year)
animate(By_year_new, 
        fps = 10, 
        height = 500, 
        width = 700,renderer = gifski_renderer("gganim.gif"),res=200)
## Warning in lapply(row_vars$frames, as.integer): NAs introduced by coercion
## Warning in split.data.frame(d, as.integer(split_panel[, 3])): NAs introduced by
## coercion
## nframes and fps adjusted to match transition